package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbCart;
import com.travel.entity.TbTrain;
import com.travel.entity.TbUser;
import com.travel.mapper.TrainMapper;
import com.travel.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, TbTrain> implements TrainService {


    @Override
    public ResultMsg selectAll(int current, int size) {
        Page<TbTrain> trainPage = new Page<>(current,size);
        Page<TbTrain> page = super.page(trainPage);
        return ResultMsg.success(page);
    }

    @Override
    public ResultMsg selectSnameByLike(String sname) {
        QueryWrapper<TbTrain> wrapper = new QueryWrapper<>();
        wrapper.like("train_sname",sname);
        List<TbTrain> list = super.list(wrapper);
        if (list==null){

            ResultMsg.error(101,"暂无该城市信息");
        }

        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg selectEnameByLike(String ename) {
        QueryWrapper<TbTrain> wrapper = new QueryWrapper<>();
        wrapper.like("train_ename",ename);
        List<TbTrain> list = super.list(wrapper);
        if (list==null){
            ResultMsg.error(100,"暂无该城市信息");
        }

        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg selectSnameAndEnameByLike(String sname, String ename) {
        QueryWrapper<TbTrain> wrapper = new QueryWrapper<>();
        wrapper.like("train_sname",sname);
        wrapper.like("train_ename",ename);
        List<TbTrain> list = super.list(wrapper);
        if (list==null || sname.equals(ename) || sname.equals("") || ename.equals("") ){
            ResultMsg.error(104,"暂无该城市信息");
        }
        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg selectOneByTid(String tid) {

        QueryWrapper<TbTrain> wrapper = new QueryWrapper<>();
        wrapper.eq("train_id",tid);
        TbTrain tbTrain = super.getOne(wrapper);
        if (tbTrain==null){
            throw new RuntimeException("暂无该票");
        }
        return ResultMsg.success(tbTrain);
    }

    @Override
    public ResultMsg trainOrder(String tid) {
        final QueryWrapper<TbTrain> wrapper = new QueryWrapper<>();
        wrapper.eq("train_id",tid);
        TbTrain one = super.getOne(wrapper);
        return ResultMsg.success(one);
    }

    @Override
    public ResultMsg deleteOneByTid(String tid) {
        boolean byId = super.removeById(tid);
        if (!byId){
            return ResultMsg.error(102,"删除操作不当");
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateOne(TbTrain tbTrain) {

        TbTrain train = new TbTrain();
        train.setTrainId(tbTrain.getTrainId());
        train.setTrainEname(tbTrain.getTrainEname());
        train.setTrainSname(tbTrain.getTrainSname());
        train.setTrainEtime(tbTrain.getTrainEtime());
        train.setTrainStime(tbTrain.getTrainStime());
        train.setTrainNumber(tbTrain.getTrainNumber());
        train.setTrainSeat(tbTrain.getTrainSeat());
        train.setTrainPrice(tbTrain.getTrainPrice());
        train.setTrainType(tbTrain.getTrainType());
        boolean update = super.saveOrUpdate(train);

        if (!update){
            return ResultMsg.error(103,"修改有误");
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg getTrainById(String trainId) {
        TbTrain byId = super.getById(trainId);
        if(byId==null){
            return  ResultMsg.error(104,"没有当前的火车班次");
        }else {
            return ResultMsg.success(byId);
        }
    }

    @Override
    public ResultMsg updateTrain(TbTrain tbTrain) {
        boolean b = super.saveOrUpdate(tbTrain);
        if (tbTrain!=null)
            return ResultMsg.success(b);
        else
            return ResultMsg.error(105,"修改火车票失败");
    }

    @Override
    public ResultMsg deleteTrain(String id) {
        boolean b = super.removeById(id);
        if (b)
            return ResultMsg.success(b);
        return ResultMsg.error(106,"删除失败");
    }

}
